// pages/seckillOrderInfo/seckillOrderInfo.js
const app = getApp();
import api from '../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    discount: app.globalData.discount,
    navmess:{
      navtitle: "填写订单信息",
      backhide: 0,
      topheight: 1
    },
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  // 拼团复选框
  checkboxclick: function(){
    var demolist = this.data.demolist;
    demolist.selected = !demolist.selected;
    this.setData({
      demolist: demolist
    })
  },
  bindMinus: function (e) {
    var that = this;
    var type = this.data.type;
    if(type == 1){
      var index = parseInt(e.currentTarget.dataset.index),
        quantity = this.data.demolist[index].quantity,
        id = this.data.demolist[index].id;
    var demolist = this.data.demolist;
    if (demolist[index].seckillId !== null) {
      var price = demolist[index].seckillPrice
    } else {
      var price = demolist[index].price
    }
    if(quantity>0){
      quantity--;
      this.setData({
        count: this.data.count - 1 * price,
        number: this.data.number - 1
      });
      var params = {
        "token": this.data.token,
        "id": id,
        "quantity": quantity
      }
      api.post("/mini/my/cart/modifyCart",params).then(res =>{
        if (res.code == 200) {
            
          }
        }).catch(res => {
          console.log("请求失败：",res);
          return false;
        })
      }
      demolist[index].quantity = quantity;
      this.setData({
        demolist: demolist
      });
    }else if(type == 2 ){
      var demolist = this.data.demolist;
      if(demolist.buyQuantity >1){
        demolist.buyQuantity--;
        that.setData({
          demolist: demolist
        })
        api.post("/mini/my/pay/spellGroupSettlement",{
          "token": that.data.token,
          "activityNo": that.data.activityNo,
          "buyQuantity": that.data.demolist.buyQuantity
        }).then(res => {
        that.setData({
          demolist: res.result
        })
        }).catch(res => {
          console.log(res)
        })
      }else{
        wx.showToast({
          title: '至少选择一件商品',
          icon: "none",
          duration: 1500
        })
      }
    }else if(type == 6){
      var demolist = this.data.demolist;
      if(demolist.buyQuantity >1){
        demolist.buyQuantity--;
        that.setData({
          demolist: demolist
        })
        api.post("/mini/my/pay/seckillSettlement",{
          "token": that.data.token,
          "activityNo": that.data.activityNo,
          "buyQuantity": that.data.demolist.buyQuantity
        }).then(res => {
        that.setData({
          demolist: res.result
        })
        }).catch(res => {
          console.log(res)
        })
      }else{
        wx.showToast({
          title: '至少选择一件商品',
          icon: "none",
          duration: 1500
        })
      }
    }
  },
  bindPlus: function (e) {
    var that = this;
    var type = this.data.type;
    if(type == 1){
      var that = this,
          index = parseInt(e.currentTarget.dataset.index),
          quantity = this.data.demolist[index].quantity,
          id = this.data.demolist[index].id;
      var demolist = this.data.demolist;
      quantity++;
      demolist[index].quantity = quantity;
      if (demolist[index].seckillId !== null) {
        var price = demolist[index].seckillPrice
      } else {
        var price = demolist[index].price
      }
      this.setData({
        count: this.data.count + 1 * price,
        number: this.data.number + 1,
        demolist: demolist
      });
      var params = {
        "token": this.data.token,
        "id": id,
        "quantity": quantity
      }
      api.post("/mini/my/cart/modifyCart",params).then(res =>{
        if (res.code == 200) {
            
        }
      }).catch(res => {
        console.log("请求失败：",res);
        return false;
      })
    }else if(type ==2){
      var demolist = this.data.demolist;
      demolist.buyQuantity++;
      that.setData({
        demolist: demolist
      })
      api.post("/mini/my/pay/spellGroupSettlement",{
        "token": that.data.token,
        "activityNo": that.data.activityNo,
        "buyQuantity": that.data.demolist.buyQuantity
      }).then(res => {
        that.setData({
          demolist: res.result
        })
      }).catch(res => {
        wx.showToast({
          title: res.message,
          icon: "none",
          duration: 1500
        })
      })
    }else if(type == 6){
      var demolist = this.data.demolist;
      demolist.buyQuantity++;
      that.setData({
        demolist: demolist
      })
      api.post("/mini/my/pay/seckillSettlement",{
        "token": that.data.token,
        "activityNo": that.data.activityNo,
        "buyQuantity": that.data.demolist.buyQuantity
      }).then(res => {
        that.setData({
          demolist: res.result
        })
      }).catch(res => {
        wx.showToast({
          title: res.message,
          icon: "none",
          duration: 1500
        })
      })
    }
  },
  // 监听编辑信息
  gettextarea: function(e){
    this.setData({
      remark: e.detail.value
    })
  },
  usernametxt: function(e){
    this.setData({
      username: e.detail.value
    })
  },
  phonetxt: function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  // 立即下单
  toObligation: function(e){
    var that =this,
        type = this.data.type,
        token = this.data.token,
        url = e.currentTarget.dataset.url,
        ent = app.globalData.ent,
        receiveSmallArea = this.data.receiveSmallArea,
        receiveAddress = this.data.receiveAddress,
        userName = this.data.userName,
        phone = this.data.phone,
        remark = this.data.remark;
    var demolist = that.data.demolist;
    // 携带参数选择
    if(type == 1){
      var goods = [];  //取商品json
      for(var i = 0;i < demolist.length;i++){
        goods[i] = {
          id: demolist[i].id,
          goodsType: demolist[i].goodsType
        }
      }
      var message = {
        goods: goods,
        ent: ent,
        receiveSmallArea: receiveSmallArea,
        receiveAddress: receiveAddress,
        userName: userName,
        phone: phone,
        remark: remark
      }
      if(that.receiveSmallArea){
        wx.navigateTo({
          url: url +"?message="+ JSON.stringify(message) +"&type="+ that.data.type,
        })
      }else{
        wx.showToast({
          title: '请选择收货地址',
          icon: "none",
          duration: 1500
        })
      }
    }else if(type == 2){
      var message = {
        token: token,
        receiveSmallArea: receiveSmallArea,
        receiveAddress: receiveAddress,
        userName: userName,
        phone: phone,
        remark: remark,
        quantity: that.data.demolist.buyQuantity,
        signSpellOrder: that.data.groupmess.signSpellOrder,
        signSpellGroup: that.data.groupmess.signSpellGroup
      }
      if(demolist.selected){   
        // 产品类小区id必填
        if(demolist.goodsType == 1){
          if(that.receiveSmallArea){
            wx.navigateTo({
              url: url +"?message="+ JSON.stringify(message) +"&type="+ that.data.type,
            })
          }else{
            wx.showToast({
              title: '请选择收货地址',
              icon: "none",
              duration: 1500
            })
          }
        }else{
          wx.navigateTo({
            url: url +"?message="+ JSON.stringify(message) +"&type="+ that.data.type,
          })
        }
      }else{
        wx.showToast({
          title: '请选择商品',
          icon: "none",
          duration: 1500
        })
      }
    }else if(type == 3){
      var message = {
        token: token,
        receiveSmallArea: receiveSmallArea,
        receiveAddress: receiveAddress,
        userName: userName,
        phone: phone,
        remark: remark,
        bargainId: that.data.bargainmess.bargainId,
        bargainOrderId: that.data.bargainmess.bargainOrderId
      }
      if(demolist.selected){   
        // 产品类小区id必填
        if(demolist.goodsType == 1){
          if(that.receiveSmallArea){
            wx.navigateTo({
              url: url +"?message="+ JSON.stringify(message) +"&type="+ that.data.type,
            })
          }else{
            wx.showToast({
              title: '请选择收货地址',
              icon: "none",
              duration: 1500
            })
          }
        }else{
          wx.navigateTo({
            url: url +"?message="+ JSON.stringify(message) +"&type="+ that.data.type,
          })
        }
      }else{
        wx.showToast({
          title: '请选择商品',
          icon: "none",
          duration: 1500
        })
      }
    }else if(type == 6){
      var message = {
        token: token,
        receiveSmallArea: receiveSmallArea,
        receiveAddress: receiveAddress,
        userName: userName,
        phone: phone,
        remark: remark,
        seckillId: that.data.demolist.id,
        quantity: that.data.demolist.buyQuantity
      }
      // 产品类小区id必填
      if(demolist.goodsType == 1){
        if(that.receiveSmallArea){
          wx.navigateTo({
            url: url +"?message="+ JSON.stringify(message) +"&type="+ that.data.type,
          })
        }else{
          wx.showToast({
            title: '请选择收货地址',
            icon: "none",
            duration: 1500
          })
        }
      }else{
        wx.navigateTo({
          url: url +"?message="+ JSON.stringify(message) +"&type="+ that.data.type,
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 接收type类型
    console.log(options)
    var token = wx.getStorageSync('token');
    this.setData({
      token: token,
      type: options.type
    })
    if(options.type == 1){
      //秒杀走购物车
      this.setData({
        selectIds: JSON.parse(options.selectIds)
      })
      api.post("/mini/my/pay/cartSettlement",{
        "token": that.data.token,
        "ids": that.data.selectIds
      }).then(res => {
        console.log(res)
        var demolist = res.result.cartList,
            count=0,number=0;

        for(var i=0;i < demolist.length;i++){
          if (demolist[i].seckillId !== null) {
            var price = demolist[i].seckillPrice
          } else {
            var price = demolist[i].price
          }
          count += Number(price) * Number(demolist[i].quantity);
          number += Number(demolist[i].quantity);
        }
        that.setData({
          demolist: demolist,
          count: count,
          number: number,
        })
      }).catch(res => {
        console.log("请求失败：",res);
        return false;
      })
    }else if(options.type == 6){
      // 秒杀结算
      var activityNo = JSON.parse(options.mess).activityNo,
          buyQuantity = JSON.parse(options.mess).buyQuantity;
      this.setData({
        activityNo: activityNo
      })
      api.post("/mini/my/pay/seckillSettlement",{
        "token": token,
        "activityNo": activityNo,
        "buyQuantity": buyQuantity
      }).then(res => {
        console.log(res)
        that.setData({
          demolist: res.result,
          number: res.result.buyQuantity
        })
      }).catch(res => {
        console.log(res)
      })
    }else if(options.type == 2){
      this.setData({
        activityNo: JSON.parse(options.mess).activityNo
      })
      api.post("/mini/my/pay/spellGroupSettlement",{
        "token": that.data.token,
        "activityNo": that.data.activityNo,
        "buyQuantity": that.data.buyQuantity
      }).then(res => {
        console.log("拼团",res)
        var demolist = res.result,
            count=0,number=0;
        count = Number(demolist.spellGroupPrice) * Number(demolist.buyQuantity);
        number = Number(demolist.buyQuantity);
        demolist.selected = false;
        that.setData({
          demolist: demolist,
          count: count,
          number: number,
        })
        that.setData({     
          groupmess: {     //拼团下单参数
            signSpellOrder: that.data.demolist.signSpellOrder,
            signSpellGroup: that.data.demolist.sign,
          }
        })
      }).catch(res => {
        console.log("请求失败：",res);
        return false;
      })
    }else if(options.type == 3){
      this.setData({
        bargainOrderSign: JSON.parse(options.mess).bargainOrderSign
      })
      api.post("/mini/my/pay/spellGroupSettlement",{
        "token": that.data.token,
        "bargainOrderSign": that.data.bargainOrderSign
      }).then(res => {
        console.log("砍价成功",res)
        that.setData({
          demolist: res.result
        })
        that.setData({     
          bargainmess: {     //砍价下单参数
            bargainId: that.data.demolist.id,
            bargainOrderId: that.data.demolist.bargainOrderId,
          }
        })
      }).catch(res => {
        console.log("请求失败：",res);
        return false;
      })
    }else if(options.type == 5){
      this.setData({
        activityNo: JSON.parse(options.mess).activityNo,
        quantity: JSON.parse(options.mess).quantity
      })
      api.post("/mini/my/pay/scoreSettlement",{
        "token": that.data.token,
        "activityNo": that.data.activityNo,
        "quantity": that.data.quantity
      }).then(res => {
        console.log("砍价成功",res)
        that.setData({
          demolist: res.result
        })
      }).catch(res => {
        console.log("请求失败：",res);
        return false;
      })
    }
    // 取默认收货信息
    api.post("/mini/my/basic/logistics",{
      "token": token
    }).then(res => {
      console.log("收货地址列表",res)
      for(var i=0;i<res.result.length;i++){
        if(res.result[i].defaulted){
          that.setData({
            userName: res.result[i].receiver,
            phone: res.result[i].receiveMobile,
            receiveSmallArea: res.result[i].id,
            receiveAddress: res.result[i].receiveAddress,
            adress: res.result[i].receiveAddress.join(' ')  //详细地址数组
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})